package com.eascs.placeonfile.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.placeonfile.entity.O2oFileEntity;
import com.eascs.placeonfile.form.O2oFileForm;
import com.eascs.placeonfile.format.O2oFileForMat;
import com.eascs.placeonfile.service.O2oFileService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.workflow.form.ZXPTCWForm;

@RestController
@RequestMapping("/ronfile")
public class O2oFileRestController extends BaseRestController {
	private static Logger log = LoggerFactory.getLogger(O2oFileRestController.class);
	@Autowired
	O2oFileService fileService;
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private O2oDataListService o2oDataListService;

	@RequestMapping("/start")
	public BaseRestResult<String> index(O2oFileForm fileForm) {
		if ("BC".equals(fileForm.getBank_type())) {
			if (StringUtil.isEmpty(fileForm.getLoan_bank_stream())) {
				return BaseRestResult.warn("请选择银行流水");
			}
			if (StringUtil.isEmpty(fileForm.getLoan_prompt_letter())) {
				return BaseRestResult.warn("请选择温馨提示函");
			}
			if (StringUtil.isEmpty(fileForm.getLoan_bc_sd_data())) {
				return BaseRestResult.warn("请选择3张SD单据");
			}
			if (StringUtil.isEmpty(fileForm.getLoan_credit())) {
				return BaseRestResult.warn("请选择征信报告");
			}
		}
		if ("GACZ".equals(fileForm.getBank_type())) {
			if (StringUtil.isEmpty(fileForm.getLoan_notice_book())) {
				return BaseRestResult.warn("请选择委托支付通知书");
			}
			if (StringUtil.isEmpty(fileForm.getLoan_house())) {
				return BaseRestResult.warn("请选择房产证明");
			}
			if (StringUtil.isEmpty(fileForm.getLoan_cz_sd_data())) {
				return BaseRestResult.warn("请选择5张SD单据");
			}
		}
		if (null == fileForm.getSend_date()) {
			return BaseRestResult.warn("请选择寄送日期");
		}
		if (fileService.checkDataCode(fileForm.getLoanapplyNo())) {
			O2oFileEntity fileEntity = fileService.getEntity(fileForm.getLoanapplyNo());
			log.debug("查询归档资料：{}", fileEntity);
			if (StringUtil.isEmpty(fileForm.getId()) && null == fileEntity) {
				O2oFileEntity entity = O2oFileForMat.isnertF(fileForm);
				int i = fileService.insert(entity);
				log.debug("查询归档资料插入数据：{}条", i);
				if (i > 0) {
					return BaseRestResult.warn("保存成功");
				}
			} else {
				fileForm.setId(fileEntity.getId());
				O2oFileEntity entity = O2oFileForMat.updateToStart(fileForm);
				int i = fileService.updateById(entity);
				log.debug("查询归档资料更新数据：{}条", i);
				if (i > 0) {
					return BaseRestResult.warn("保存成功");
				}
			}
		} else {
			return BaseRestResult.warn("请上传快递寄送凭证");
		}
		log.error("贷款单号为：【{}】归档失败,失败原因：errorMessage {}", fileForm.getLoanapplyNo(), new Throwable());
		return BaseRestResult.warn("保存失败");
	}

	@RequestMapping("/place")
	public HtmlRestResult placeonfileForm(ZXPTCWForm payform) {
		log.debug("贷款单号id：{}", payform.getReferId());
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(payform.getReferId());
		O2oFileEntity fileEntity = fileService.getEntity(payform.getReferId());
		ViewContext.put("loanapply", o2oLoanapplyEntity);
		ViewContext.put("fileEntity", fileEntity);
		return HtmlRestResult.html("workflowcheck/placeonfile");
	}

	@RequestMapping("/input")
	public HtmlRestResult getUpdateInputTime(ZXPTCWForm payform) {
		log.debug("贷款单号id：{}", payform.getReferId());
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(payform.getReferId());
		O2oFileEntity fileEntity = fileService.getEntity(payform.getReferId());
		ViewContext.put("loanapply", o2oLoanapplyEntity);
		ViewContext.put("fileEntity", fileEntity);
		return HtmlRestResult.html("workflowcheck/placeinputtime");
	}

	@RequestMapping("/updateinput")
	public BaseRestResult<String> updateInputDate(O2oFileForm fileForm) {
		if (null == fileForm.getInput_date()) {
			return BaseRestResult.warn("请选择录入时间");
		}
		O2oFileEntity entity = O2oFileForMat.updateF(fileForm);
		int i = fileService.updateById(entity);
		if (i > 0) {
			return BaseRestResult.warn("保存成功");
		}
		return BaseRestResult.warn("保存失败");
	}
	
	@RequestMapping("/fileDiv")
	public HtmlRestResult getFileDiv(String loanapplyNo,String loanId){
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("loanId", loanId);
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(loanapplyNo);
		ViewContext.put("o2oDataList", o2oDataList);
		return HtmlRestResult.html("workflowcheck/placeonfileDiv");
	}
}
